package com.liang.maogouyuyin.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.liang.maogouyuyin.R;
import com.liang.maogouyuyin.databinding.FragmentInteractBinding;
import com.liang.maogouyuyin.ui.adapter.InteractFragmentAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class InteractFragment extends Fragment {
    FragmentInteractBinding binding;
    @SuppressLint("StaticFieldLeak")
    public static FrameLayout mFeedContainer;

    private InteractFragmentAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentInteractBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView();
    }

    private void initView() {
        adapter = new InteractFragmentAdapter(requireActivity());
        binding.interactViewPager.setAdapter(adapter);

        new TabLayoutMediator(binding.interactCenterTabLayout, binding.interactViewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText(getString(R.string.interact_quotations));
                    break;
                case 1:
                    tab.setText(getString(R.string.interact_instruction));
            }
        }).attach();

        binding.interactTopTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0) {
                    QuotationsFragment quotationsFragment = (QuotationsFragment) adapter.getFragment(0);
                    quotationsFragment.setDog(false);
                    if (adapter.getListSize() == 2) {
                        InstructionFragment instructionFragment = (InstructionFragment) adapter.getFragment(1);
                        instructionFragment.setDog(false);
                    }
                } else {
                    QuotationsFragment quotationsFragment = (QuotationsFragment) adapter.getFragment(0);
                    quotationsFragment.setDog(true);
                    if (adapter.getListSize() == 2) {
                        InstructionFragment instructionFragment = (InstructionFragment) adapter.getFragment(1);
                        instructionFragment.setDog(true);
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
